//index.js
//获取应用实例
const app = getApp()
var time = require("../../utils/util.js");
import Dialog from '../../@vant/weapp/dialog/dialog';
Page({
  data: {
    nickName: '',
    avatarUrl: '../../image/mypic.png',
    show1: false,
    phone: ''
  },
  login: function(){
    var that = this;
    wx.login({
      success: res => {
        console.log(res)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          //获取openid接口  
          url: time.httpurl + '/auth/app/login',
          data: {
            "code": res.code,
            "phone":  that.data.phone
          },
          method: 'POST',
          success: function (res) {
            console.log(res);
            if(res.data.code == 200){
              wx.setStorageSync("openId",res.data.data.openId);
              wx.setStorageSync("token",res.data.data.access_token);
              that.setData({
                show1: false
              })
            }else{
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }            
          }
        })
      }
    })
  },
  bindnow: function(){
    var that = this;
     wx.navigateTo({
       url: '../details/details',
     })
  },
  bindsearch: function(){
    var that = this;
    wx.request({
      //获取openid接口  
      url: time.httpurl + '/event/app/getUserInfo',
      method: 'get',
      header: {
        'AppType': '1',
        'Authorization': wx.getStorageSync("token"),
      },
      success: function (res) {
        console.log(res);
        if(res.data.code == 200){
          if(res.data.data.appRole == 1){
            wx.navigateTo({
              url: '../eventleaderlist/eventleaderlist',
            })
          }else{
            wx.navigateTo({
              url: '../eventlist/eventlist',
            })
          }
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }            
      }
    })
  },
  binddaily: function(){
    var that = this;
    wx.request({
      //获取openid接口  
      url: time.httpurl + '/event/app/getUserInfo',
      method: 'get',
      header: {
        'AppType': '1',
        'Authorization': wx.getStorageSync("token"),
      },
      success: function (res) {
        console.log(res);
        if(res.data.code == 200){
          if(res.data.data.appRole == 1){
            wx.navigateTo({
              url: '../dailyleaderlist/dailyleaderlist',
            })
          }else{
            wx.navigateTo({
              url: '../dailylist/dailylist',
            })
          }
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }            
      }
    })
  },
   /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;    
    if(!wx.getStorageSync('token')){
      that.setData({
        show1: true
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      //获取openid接口  
      url: time.httpurl + '/event/app/getUserInfo',
      method: 'get',
      header: {
        'AppType': '1',
        'Authorization': wx.getStorageSync("token"),
      },
      success: function (res) {
        console.log(res);
        if(res.data.msg == "登录状态已过期"){
          that.setData({
            show1: true
          })
        }          
      }
    })
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})
